package com.fxz.oauth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Fxz
 * @version 1.0
 * @date 2021-08-04 16:22
 */
@Configuration
public class PasswordEncoderConfig {

    /**
     * 密码编码
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        //默认的编码方式
        String idForDefault = "bcrypt";

        //储存两种编码
        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put(idForDefault, new BCryptPasswordEncoder());
        encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));

        //实现多种编码共存
        return new DelegatingPasswordEncoder(idForDefault, encoders);
    }

}
